package com.anbinh.doctor.fragment.clinic;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.model.AppointMentDetailt;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.LichType;
import com.anbinh.doctor.utils.Utils;

public class FragmentClinicAppointmentDetailt extends Fragment {
	private static FragmentClinicAppointmentDetailt fragment = null;
	private View mView;
	private View mLoadding;
	private UserLogin userLogin;

	private TextView lblTime;
	private TextView lblBenhnhan;
	private TextView lblPhone;
	private TextView lblEmail;
	private TextView lblLydo;
	private View lineOptionChoduyet;
	private Button btnDongy;
	private Button btnTuchoi;
	private int status;
	protected String startTime;
	protected String endTime;
	private int doctorid;
	private static int mId;
	

	public static FragmentClinicAppointmentDetailt newInstance(Bundle bundle) {
		if (fragment == null)
			fragment = new FragmentClinicAppointmentDetailt();
		mId = bundle.getInt(Utils.KEY_ID);
		
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_chitiet_henkham, null);
		doctorid= FragmentDoctorList.newInstance().getCurrentDoctors().getId();
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		lineOptionChoduyet = (LinearLayout) getView().findViewById(
				R.id.lineOptionChoduyet);
		mLoadding = (View) mView.findViewById(R.id.loadding);
		lblTime = (TextView) mView.findViewById(R.id.lblTime);
		lblBenhnhan = (TextView) mView.findViewById(R.id.lblBenhnhan);
		lblPhone = (TextView) mView.findViewById(R.id.lblPhone);
		lblEmail = (TextView) mView.findViewById(R.id.lblEmail);
		lblLydo = (TextView) mView.findViewById(R.id.lblLydo);
		btnDongy = (Button) mView.findViewById(R.id.btnDongy);
		btnTuchoi = (Button) mView.findViewById(R.id.btnTuchoi);
		btnTuchoi = (Button) mView.findViewById(R.id.btnTuchoi);

		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);
		if (userLogin == null)
			return;
		

		mLoadding.setVisibility(View.VISIBLE);
		lineOptionChoduyet.setVisibility(View.GONE);

		ServerController.getInstance(getActivity()).requestAppointmentDetailt(
				userLogin.getData().getToken(), mId + "",doctorid,
				new OnRequest<AppointMentDetailt>() {

					@Override
					public void onSuccess(AppointMentDetailt data) {
						Utils.showMessage(getActivity(), "Thành công");
						mLoadding.setVisibility(View.INVISIBLE);
						if (data.getResponsecode() >= 0) {
							String tmp = data.getData().getStatus();
							if (tmp.isEmpty()) {
								status = Utils.LICH_TRONG;// Lịch khám trống
							} else {
								status = Integer.parseInt(tmp);
							}

							if (status == LichType.LICH_CHODUYET_MAUVANG
									.getCode()
									|| (status == LichType.LICH_DADAT_MAUDO
											.getCode())) {
								lineOptionChoduyet.setVisibility(View.VISIBLE);
							} else {
								lineOptionChoduyet.setVisibility(View.GONE);
							}

							if (status == LichType.LICH_DADAT_MAUDO.getCode()) {
								btnTuchoi.setText("Hủy lịch");
								btnTuchoi
										.setBackgroundResource(R.drawable.bgr_lichhen_mauxam);
								btnTuchoi.setTextColor(Color
										.parseColor("#3f3f3f"));
								btnDongy.setText("Đã khám");
							}

							lblTime.setText(data.getData().getStart_time()
									+ "-" + data.getData().getEnd_time());

							lblBenhnhan.setText(data.getData().getName());
							lblPhone.setText(data.getData().getPhone());
							lblEmail.setText(data.getData().getEmail());
							lblLydo.setText(data.getData().getVisit_reason());

						}

					}

					@Override
					public void onError(int errorCode, String message) {
						mLoadding.setVisibility(View.INVISIBLE);
						Utils.showMessage(getActivity(), "Lỗi");
					}
				});

		btnDongy = (Button) mView.findViewById(R.id.btnDongy);
		btnDongy.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (status == LichType.LICH_CHODUYET_MAUVANG.getCode()) {
					onUpdateStatus(1);// dong y
				} else if (status == LichType.LICH_DADAT_MAUDO.getCode()) {
					onUpdateStatus(2);// dong y
				}
			}
		});

		btnTuchoi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onUpdateStatus(-1);// Từ chối, Hủy Lịch
			}
		});
	}

	public void onUpdateStatus(int status) {
		mLoadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);

		ServerController.getInstance(getActivity()).requestUpdateStatusLichhen(
				userLogin.getData().getToken(), mId, status,doctorid,
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						mLoadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);

						if (data.getResponsecode() >= 0) {
							Intent intent = new Intent();
							intent.putExtra(Utils.KEY, Utils.ONRESUME_LICHHEN);
							getActivity().setResult(Integer.MAX_VALUE, intent);
							getActivity().finish();
						}
					}

					@Override
					public void onError(int errorCode, String message) {
						mLoadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);

					}

				});
	}

	public void updateInfo() {
		mLoadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);

	}

}
